#!/bin/bash
# running from a git repo
# ansible files exist
CWD=`pwd`
export ANSIBLE_LOG_PATH="$CWD/iiab-network.log"

exit_error() {
    echo -e "\nEXITING: "$@ | tee -a /opt/iiab/iiab/iiab-network.log
    exit 1
}

if [ ! -f iiab-network.yml ]; then
    exit_error "iiab-network.yml not found in current directory." \
               "Please rerun this command from the top level of the git repo."
fi

OS="unknown"   # will be overridden below, if /etc/iiab/iiab.env is legit
if [ -f /etc/iiab/iiab.env ]; then
    echo "Reading /etc/iiab/iiab.env"
    STAGE=0
    source /etc/iiab/iiab.env
    if grep -q STAGE= /etc/iiab/iiab.env ; then
        echo -e "\nExtracted STAGE=$STAGE (counter) from /etc/iiab/iiab.env"
        if ! [ "$STAGE" -eq "$STAGE" ] 2> /dev/null; then
            exit_error "STAGE (counter) value == ""$STAGE"" is non-integer"
        elif [ "$STAGE" -lt 0 ] || [ "$STAGE" -gt 9 ]; then
            exit_error "STAGE (counter) value == ""$STAGE"" is out-of-range"
        elif [ "$STAGE" -lt 3 ]; then
            exit_error "STAGE (counter) value == ""$STAGE" \
                       "\nIIAB Stage 3 not complete." \
                       "\nPlease run: ./iiab-install"
        fi
    else
        exit_error "STAGE (counter) not found" \
                   "\nIIAB not installed." \
                   "\nPlease run: ./iiab-install"
    fi
else
    exit_error "/etc/iiab/iiab.env not found"
fi

echo "Ansible will now run iiab-network.yml -- log file is iiab-network.log"
Start=`date`
ansible -m setup -i ansible_hosts localhost --connection=local | grep python
ansible-playbook -i ansible_hosts iiab-network.yml --extra-vars "{\"skip_role_on_error\":false}" --connection=local
End=`date`


# Record critical diagnostics to [/opt/iiab/iiab/]iiab-network.log
echo >> iiab-network.log

# redhat path
# Paul Armstrong's Shell Style Guide (https://google.github.io/styleguide/shell.xml)
# prefers "if [[ ... ]]; then" for REGEXP's.  Many others prefer "if [ ... ];" then.
# Each approach is sometimes necessary in my experience, working differently indeed.
if [ "$OS" == "centos" ] || [ "$OS" == "fedora" ]; then
    ls -la /etc/sys*/net*/ifcfg* >> iiab-network.log
fi

# Ubuntu desktop/others might be using NM (NetworkManager) - split out.
#if [ $(grep ubuntu /etc/apt/sources.list) ]; then  # FAILS when multiple lines returned, due to single square brackets
#if grep -q ubuntu /etc/apt/sources.list ; then     # Works: bypasses need for "> /dev/null" thanks to "grep -q" (quiet)
#if [[ `which nmcli` ]]; then            # "which" misses built-in commands like cd, and is RISKY per https://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
#if [[ `type -P nmcli` ]]; then          # "type -P" isn't POSIX compliant; it misses built-in commands like "cd"
#if [[ $(command -v nmcli) ]]; then      # Works!  And $(...) nests more easily than backticks.
if command -v nmcli > /dev/null; then    # "command -v" is POSIX compliant; also catches built-in commands like "cd"
    nmcli d >> iiab-network.log
    echo >> iiab-network.log
    nmcli c >> iiab-network.log
    echo >> iiab-network.log
fi
if command -v networkctl > /dev/null; then
    networkctl >> iiab-network.log
    echo >> iiab-network.log
fi

ip r >> iiab-network.log
bridge -d link >> iiab-network.log
echo >> iiab-network.log
echo "iiab-network run start: $Start" >> iiab-network.log
echo "iiab-network run end: $End" >> iiab-network.log
echo >> iiab-network.log
echo >> iiab-network.log


# Put the same diagnostics on screen, for live operator
if command -v nmcli > /dev/null; then
    nmcli d
    echo
    nmcli c
    echo
fi
if command -v networkctl > /dev/null; then
    networkctl
    echo
fi
ip r
bridge -d link
echo
echo "iiab-network run start: $Start"
echo "iiab-network run end: $End"
echo
echo "Please REBOOT to fully verify your network -- graphical desktops MUST reboot!"
exit 0
